package paperockscissors;

import java.util.ArrayList;
import java.util.Random;

public class Player {

	// Attributes
	private String name;
	private int score;
	private int countMoves;
	private ArrayList<RPSEnum> moves;

	// Constructor 1
	public Player(String name, ArrayList<RPSEnum> moves) {
		this.name = name;
		this.score = 0;
		this.countMoves = 0;
		this.moves = moves;
	}

	// Constructor 2
	public Player(String name) {
		this(name, null);

		// collection of moves
		Random random = new Random();

		int nbMoves = RandomInteger(10, 20, random);

		moves = new ArrayList<RPSEnum>(nbMoves);
		
		for (int i = 0; i < nbMoves; i++) {
			// Add movement (number between 0 and 2)
			switch (random.nextInt(2)) {
			case 0:
				moves.add(RPSEnum.PAPER);
				break;
			case 1:
				moves.add(RPSEnum.ROCK);
				break;
			case 2:
				moves.add(RPSEnum.SCISSORS);
				break;
			}
		}
	}

	// Randomize integer with a predefined range
	public int RandomInteger(int aStart, int aEnd, Random aRandom) {
		if (aStart > aEnd) {
			throw new IllegalArgumentException("Start cannot exceed End.");
		}
		// get the range, casting to long to avoid overflow problems
		long range = (long) aEnd - (long) aStart + 1;
		// compute a fraction of the range, 0 <= frac < range
		long fraction = (long) (range * aRandom.nextDouble());
		int randomNumber = (int) (fraction + aStart);
		return randomNumber;
	}

	// RPSEnum Method for the next movement
	public RPSEnum getNextMove() {
		RPSEnum r = moves.get(this.countMoves);
		countMoves++;
		return r;
	}
	
	public void resetMovesCount() {
		this.countMoves = 0;
	}

	// Getters
	public String getName() {
		return name;
	}

	public int getScore() {
		return score;
	}

	public int getNbMoves() {
		return moves.size();
	}

	// Setter
	public void setScore(int score) {
		this.score = score;
	}

}